package labox.innovation.gameserver.templates.item;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.idfactory.IdFactory;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2ItemInstance.ItemGradeType;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

/**
 * ��Ʒ�����
 * 
 * @author zjj
 * 
 */
public class ItemCreateFactory {
	static Logger _log = LoggerFactory.getLogger(ItemCreateFactory.class.getName());

	// ���� 2���ֶ� ��ʾ �ǹ������
	protected static final int NON_MOB_GRADE = -1;
	protected static final int NON_MOB_LVL = -1;

	protected static String PROCESS_CMD = "Init";
	
	public static int ETCITEM_GRADE = -8;
	public static String ITEM_OP = "������Ʒ:";


	public static final ItemCreateFactory getInstance() {
		return SingletonHolder._instance;
	}

	/**
	 * �����Ʒ(�ǹ���������� ����)
	 * 
	 * @param process
	 * @param itemId
	 * @param count
	 * @param actor
	 * @param reference
	 * @return
	 */
	public L2ItemInstance createItem(String process, int itemId, int count, FuzePcInstance actor, L2Object reference) {
		return createItem(process, itemId, count, actor, reference, NON_MOB_GRADE, NON_MOB_LVL);
	}

	/**
	 * �����Ʒ ������� Ӧ��ֱ�ӵ��ô˺���
	 * 
	 * @param process
	 * @param itemId
	 *            : ��Ʒģ��Id
	 * @param count
	 *            : ��ɵ�����
	 * @param actor
	 * @param reference
	 *            mobgrade : ���ﾫӢ�ȼ� moblvl : ����ȼ�
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public L2ItemInstance createItem(String process, int itemId, int count, FuzePcInstance actor, L2Object reference, int mobgrade, int moblvl) {
		// ��Ʒģ�� �Ƿ����
		L2Item itemt = ItemTable.getInstance().getTemplate(itemId);
		if (itemt == null) {
			_log.warn("��Ҫ�����Ʒģ�岻���� .ģ��idΪ:" + itemId, Config.SERVER_ID  );
			return null;
		}

		Enum type = itemt.getItemType();
		// �����Ʒ���͵�Ȩֵ
		int iweight = 0;
		if (type instanceof L2EtcItemType) {
			// ����ǵ��� ��ôֱ����ɵ��߷���
			return createItem(process, itemt, count, ETCITEM_GRADE);
		} else if (type instanceof L2ArmorType) {
			iweight = ((L2ArmorType) type).getWeight();
		} else if (type instanceof L2WeaponType) {
			iweight = ((L2WeaponType) type).getWeight();
		}

		// ��� ��Ʒ��Ʒ��, �����ƷƷ�� û�ж��� itemgrade������ -1
		// ���û�ж�����ƷƷ�ʣ�����Ʒ�ǹ������ģ� ��ô��Ҫ��ù������� ��ƷƷ��
		// ����ǹ�������װ���� ��ôĬ��װ����Ʒ��Ϊ ��ͼ� ����Ϊ����
		int itemgrade = itemt.getItemGrade();
		// �Ƕ��ƵĻ� ,�����ɹ������Ļ�
		// ��ô��� ��Ʒ��Ʒ��
		if (itemgrade == -1 && (mobgrade != NON_MOB_GRADE || moblvl != NON_MOB_LVL)) {
			// ��� ��ƷƷ�� �� ������ �������Ʒ
			itemgrade = MobItemCreateConfig.getInstance().getMobItemGrade(mobgrade, moblvl);
			if (itemgrade == -1) {
				_log.warn("װ��Ʒ�ʴ���", Config.SERVER_ID);
				return null;
			}
		} // Ĭ��Ʒ�� Ϊ����
		else if (itemgrade == -1) {
			itemgrade = ItemGradeType.TALISMAN.ordinal();
		}

		L2ItemInstance item = createItem(process, itemt, count, itemgrade);
		if( item == null ){
			return null;
		}

		// ��� ��Ҫ���� �������ֵ
		if (ItemGradeType.values()[itemgrade].getRandAttiNum() > 0 || itemt.getRandProperty() != null) {
//			System.out.println(item.getItemName() + "����װ����������");
			ItemRandAttribute itemrandatti = new ItemRandAttribute(itemt.getRandProperty(), itemt.getRandTypeAttri(), itemgrade, itemt.getItemLvl(), iweight);
			item.addItemBouns(itemrandatti);
		}

		if (item.getItem().getMusIdnetify() != -1) {
//			System.out.println(item.getItemName() + "����װ����");
			ItemIdentifyManage.getInstance().identify(item, item.getItem().getMusIdnetify(), ItemIdentifyManage.IDENTIFY_ID);
			item.setIdentify(true);
		}

		if (item.getItem().getMustInscribe() != -1) {
//			System.out.println( item.getItemName() +"����װ�����");
			ItemIdentifyManage.getInstance().identify(item, item.getItem().getMustInscribe(), ItemIdentifyManage.INSCRIBE_ID);
			item.setInscribe(true);
		}

		if (item.getItem().getMustStarLvl() > 0) {
//			System.out.println( item.getItemName() + "����װ��׷��");
			ItemStarManger.getInstance().starItem(item, item.getItem().getMustStarLvl());
		}

		return item;

	}// end createItem

	/**
	 * ���� ��Ʒ��������Ʒ�ŵ���Ϸ����
	 * 
	 * @param itemt
	 *            ��Ʒģ��
	 * @param count
	 *            ��Ʒ��
	 * @param itemgrade
	 *            ��ƷƷ��
	 * @return ��Ʒʵ��
	 */
	protected L2ItemInstance createItem(String process, L2Item itemt, int count, int itemgrade) {
		L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(itemt.getItemCalId()), itemt);
		item.setItemGrade(itemgrade);
//		_log.info( "����µ���Ʒ����ƷOBJId: {} ��Ʒ��{}" , item.getObjectId() , item );

		// ����Ϸ������ ���������Ʒ
		// ����ǳ�ʼ�� ����Ҫ����������
		if (!process.equals(PROCESS_CMD)) {
			if( _log.isDebugEnabled() )
				_log.debug("�µ���Ʒ��{} ��������", item );
			FuzeWorld.getInstance().enterWorld(item);
		}

		if (item.isStackable() && count > 1)
			item.setCount(count);
		
		_log.info(ITEM_OP + process + item.getName() + "(" + item.getObjectId() + ")" + "����:" + item.getCount() , item.getObjectId(), item.getName() );

		return item;
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final ItemCreateFactory _instance = new ItemCreateFactory();
	}

}
